/**
 * 定义站点地图模块
 */
(function (window, angular, undefined) {
    var module = angular.module('water.apps.sw.bcSendPlatform.bcSendPlatformManager',['water.entity.bcSendPlatform',
                                                                                      'water.entity.bcMonitorParamDict',
                                                                                      'water.entity.bcSendPlatformParam',
                                                                                      'water.entity.strategy',
                                                                                      'water.entity.heartBeat',
                                                                                      'water.entity.protocol']);

    module.controller('bcSendPlatformManagerController', function ($scope,$state,$bcSendPlatformService,
    										$bcMonitorParamDictService,
    										$bcSendPlatformParamService,
    										$heartBeatService,
    										$strategyService, 
    										$protocolService) {

        $scope.showId = 0;
//        $scope.protocol = "HJ212";
        $scope.ModelList = [];
        $scope.paramDict = [];
        $scope.paramDictMap = {};
        $scope.ProtocolMapList = {};
        $scope.StrategyMapList = {};
        var dateType = {RT: "秒", MINUTES: "分钟", HOUR: "小时", DAY: "天"};
        
        /**
         * 心跳策略
         */
        $scope.heartbeats = [];
        
        $scope.CreateModel = {};
        $scope.EditModel = {};
        $scope.DetailModel = {};

        $scope.shouldShow = function (showId) {
            if ($scope.showId == showId) {
                return true;
            } else {
                return false;
            }
        };
        $scope.setShowId = function (showId) {
            $scope.showId = showId;
        };

        $scope.onCreateModel = function () {
            $scope.setShowId(1);
        };

        $scope.onCreateModelSubmit = function (isCreate) {
            if (isCreate) {
                $scope.CreateModel.DAY_ST_TABLE = "BD_S_DAY";//天策略暂时固定
                $scope.CreateModel.DAY_ST_ID = "S_DAY";//天策略暂时固定
                
                for(var i=0;i<$scope.protocols.length;i++){
                	
                	if($scope.CreateModel.TRANSPORT_PROTOCOL_ID == $scope.protocols[i].k)
                		$scope.CreateModel.TRANSPORT_PROTOCOL_NAME = $scope.protocols[i].v;
            	}
                
                $scope.CreateModel.PLATFORM_ID = generateUUID();
                console.log("onCreateModel", $scope.CreateModel);
                $bcSendPlatformService.saveModel($scope.CreateModel).then(function (data) {
                    if (data) {
                        var tempModel = $scope.CreateModel;
                        var models = [];
                        for (var i = 0; i < tempModel.PARAM_CODE_BUS.length; i++) {
                            var param = {};
                            param.PLATFORM_ID = $scope.CreateModel.PLATFORM_ID;
                            param.PARAM_CODE_BUS = tempModel.PARAM_CODE_BUS[i];
                            param.ID = param.PLATFORM_ID + "_" + param.PARAM_CODE_BUS;
                            models.push(param);
                        }
                        mulitSaveModels(models, 0, function () {
                            console.log("关联参数表插入成功");
                        });
                    }
                    $scope.CreateModel = {};
                    $scope.setShowId(0);
                    $scope.getList();
                }).catch(function(msg){
                	alert(msg);
                })
            } else {
                $scope.CreateModel = {};
                $scope.setShowId(0);
            }
        };

        $scope.onDeleteModelSubmit = function (model) {
            $bcSendPlatformService.deleteModel(model).then(function (data) {
                console.log("onDeleteModelSubmit", data);
                $scope.getList();
                $bcSendPlatformParamService.deleteModel(model).then(function (data) {
                });
            });
        };

        $scope.onEditModel = function (model) {

            $scope.EditModel = model;
                                   
            $bcSendPlatformParamService.getModelList(model.PLATFORM_ID).then(function (data) {
                $scope.EditModel.PARAM_CODE_BUS = [];
                for (var i = 0; i < data.length; i++) {
                    $scope.EditModel.PARAM_CODE_BUS.push(data[i].PARAM_CODE_BUS);
                }
                $scope.setShowId(2);
            });
        };

        $scope.onEditModelSubmit = function (isEdit) {

            for(var i=0;i<$scope.protocols.length;i++){
            	
            	if($scope.EditModel.TRANSPORT_PROTOCOL_ID == $scope.protocols[i].k)
            		$scope.EditModel.TRANSPORT_PROTOCOL_NAME = $scope.protocols[i].v;
        	}
            
            if (isEdit) {
                $bcSendPlatformService.updateModel($scope.EditModel).then(function (data) {
                    $scope.getList();
                    $bcSendPlatformParamService.deleteModel($scope.EditModel).then(function (data) {
                        var tempModel = $scope.EditModel;
                        
                        var models = [];
                        
                        for (var i = 0; i < tempModel.PARAM_CODE_BUS.length; i++) {
                            var param = {};
                            param.PLATFORM_ID = tempModel.PLATFORM_ID;
                            param.PARAM_CODE_BUS = tempModel.PARAM_CODE_BUS[i];
                            param.ID = param.PLATFORM_ID + "_" + param.PARAM_CODE_BUS;
                            models.push(param);
                        }
                        mulitSaveModels(models, 0, function () {
                            $scope.EditModel = {};
                            $scope.setShowId(0);
                            $scope.getList();
                        });
                    });
                }).catch(function(msg){
                	alert(msg);
                });
            } else {
                $scope.EditModel = {};
                $scope.setShowId(0);
            }
        };
        
        /**
         * 数据策略编辑事件
         */
        $scope.onStrategyEditClick = function(model){
        	$state.go('bcSendPlatformDataStrategy',{platformId:model.PLATFORM_ID});
        }

        function mulitSaveModels(models, i, callback) {
            if (i < models.length) {
                $bcSendPlatformParamService.saveModel(models[i]).then(function (data) {
                    mulitSaveModels(models, i + 1, callback);
                });
            } else {
                callback();
            }
        }

        $scope.getList = function () {
            $bcSendPlatformService.getModelList().then(function (data) {
                $scope.ModelList = data;
                console.log("$scope.ModelList", $scope.ModelList);
            });
        };

        /**
         * 获取监测项字典数据
         */
        $scope.getParamDict = function () {
            $bcMonitorParamDictService.getList().then(function (data) {
                for (var i = 0; i < data.length; i++) {
                    $scope.paramDict.push({
                        k: data[i].PARAM_CODE_BUS,
                        v: data[i].PARAM_NAME
                    });
                    $scope.paramDictMap[data[i].PARAM_CODE_BUS] = data[i].PARAM_NAME;
                }
                console.log("$scope.paramDict", $scope.paramDict);
                $scope.ParamModel.setData($scope.paramDict);
            });
        };
        
        /**
         * 获取心跳策略
         */
        $scope.getHeartbeat = function(){
        	$heartBeatService.getList().then(function(data){
        		var hs = [];
        		
        		for(var i=0;i<data.length;i++){
        			var item = data[i];
        			hs.push({
        				k:item.id,
        				v:item.name
        			});
        		}
        		
        		$scope.heartbeats = hs;
        		$scope.heartbeatComModel.setData(hs);
        		$scope.editheartbeatComModel.setData(hs);
        	});
        }

        $scope.getStrategyMapList = function () {
            $strategyService.getList().then(function (data) {

                var rtData = [];
                var minData = [];
                var hourData = [];
                var dayData = [];

                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    item.k = item.strategyId;
                    item.v = item.strategyName;
                    if (item.dataType == 'HOUR') {
                        hourData.push(item);
                    }
                    else if (item.dataType == 'MINUTES') {
                        minData.push(item);
                    }
                    else if (item.dataType == 'SECOND') {
                        rtData.push(item);
                    }
                    else if (item.dataType == 'DAY') {
                    	dayData.push(item);
                    }
                }

                $scope.RTComModel.setData(rtData);
                $scope.aRTComModel.setData(rtData);
                $scope.MINComModel.setData(minData);
                $scope.aMINComModel.setData(minData);
                $scope.HOURComModel.setData(hourData);
                $scope.aHOURComModel.setData(hourData);
                $scope.DAYComModel.setData(dayData);
                $scope.aDAYComModel.setData(dayData);
            });
        };

        $scope.getProtocolMapList = function () {
            $protocolService.getList().then(function (data) {

                var protocols = [];

                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    protocols.push({
        				k:item.id,
        				v:item.name
        			});
                }
                $scope.protocols = protocols;
                $scope.ProtocolComModel.setData(protocols);
                $scope.aProtocolComModel.setData(protocols);

            });
        };

        $scope.onRTStrategyChange = function (value) {
            if (value == null) return;
            $scope.CreateModel.RT_ST_TABLE = value.strategyTable;
            $scope.EditModel.RT_ST_TABLE = value.strategyTable;
        };

        $scope.onMinStrategyChange = function (value) {
            if (value == null) return;
            $scope.CreateModel.MIN_ST_TABLE = value.strategyTable;
            $scope.EditModel.MIN_ST_TABLE = value.strategyTable;
        };

        $scope.onHourStrategyChange = function (value) {
            if (value == null) return;
            $scope.CreateModel.HOUR_ST_TABLE = value.strategyTable;
            $scope.EditModel.HOUR_ST_TABLE = value.strategyTable;
        };

        function generateUUID() {
            var d = new Date().getTime();
            var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d / 16);
                return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
            });
            return uuid;
        };

        $scope.init = function () {
            $scope.getList();
            $scope.getHeartbeat();
            $scope.getParamDict();
            $scope.getProtocolMapList();
            $scope.getStrategyMapList();
        };
        $scope.init();
    });

}(window, angular));


